/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author joep
 */
import com.bayu.common.lib.CommonUtil;
import com.bayu.services.dao.AvgStopTimeDao;
import com.bayu.services.dao.GraphCsHollowDao;
import com.bayu.services.dao.GraphDisaDao;
import com.bayu.services.entity.AvgStopTime;
import com.bayu.services.entity.GraphCsHollow;
import com.bayu.services.entity.GraphDisa;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/graph-disa-update.htm")
public class GraphDisaUpdate {

    private GraphDisaDao dao;
    private AvgStopTimeDao avgStopTimeDao;

    @Autowired
    public void setAvgStopTimeDao(AvgStopTimeDao avgStopTimeDao) {
        this.avgStopTimeDao = avgStopTimeDao;
    }

    @Autowired
    public void setDao(GraphDisaDao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
        List list = this.dao.getById(Long.parseLong(id));
        map.addAttribute("data", list);
    }

     @RequestMapping(method=RequestMethod.POST)
    public String processForm(@ModelAttribute("GraphDisa") GraphDisa gDisa,
            BindingResult result, ModelMap map, String date, HttpServletRequest request) {
        DateFormat format = new SimpleDateFormat("yy-MM-dd");
        Date tanggal;

        if (result.hasFieldErrors("date")) {
            try {
                tanggal = format.parse(date);
                gDisa.setDate(tanggal);
            } catch (Exception e) {
            }
        }

        String id = request.getParameter("id");

        if (id != null) {
            gDisa.setId(Long.parseLong(id));
            this.dao.update(gDisa);

             List curAvg1 = dao.custom("select avg(actualdisa) from GraphDisa"+
                         " where YEAR(date) = '"+
                         Integer.toString(CommonUtil.getCurrentYear()) +"'");
             List curAvg2 = dao.custom("select avg(batasmax) from GraphDisa"+
                         " where YEAR(date) = '"+
                         Integer.toString(CommonUtil.getCurrentYear()) +"'");
             List curAvg3 = dao.custom("select avg(frekwensi) from GraphDisa"+
                         " where YEAR(date) = '"+
                         Integer.toString(CommonUtil.getCurrentYear()) +"'");

             String strcurAvg1 = curAvg1.get(0).toString();
             String strcurAvg2 = curAvg2.get(0).toString();
             String strcurAvg3 = curAvg3.get(0).toString();

             saveAvg(CommonUtil.getCurrentYear(), Double.parseDouble(strcurAvg1), "actual DISA");
             System.out.println("Rata2 actual Disa th "+
                   Integer.toString(CommonUtil.getCurrentYear())+" = "+strcurAvg1);

             saveAvg(CommonUtil.getCurrentYear(), Double.parseDouble(strcurAvg2), "batas max DISA");
             System.out.println("Rata2 batasMAX DISA th "+
                   Integer.toString(CommonUtil.getCurrentYear())+" = "+strcurAvg2);


             saveAvg(CommonUtil.getCurrentYear(),Double.parseDouble(strcurAvg3),"frekwensi DISA");
             System.out.println("Rata2 frekwensi DISA th "+
                   Integer.toString(CommonUtil.getCurrentYear())+" = "+strcurAvg2);
            return "redirect:view-disa-data.htm?msg=Data has been updated";
        } else {
            return "redirect:graph-disa-update.htm";
        }
    }

    private void saveAvg(int tahun, double average, String jenis){
        List svAvg = avgStopTimeDao.custom("from AvgStopTime where "+
                "tahun= "+Integer.toString(tahun)+
                " AND jenis='"+jenis+"'");
        AvgStopTime inpAvg;
        if (svAvg.size() > 0) {
            inpAvg = (AvgStopTime)svAvg.get(0);
            this.avgStopTimeDao.delete(inpAvg);
        }
        inpAvg = new AvgStopTime();
        inpAvg.setTahun(tahun);
        inpAvg.setJenis(jenis);
        inpAvg.setAverage(average);

        this.avgStopTimeDao.save(inpAvg);
    }

}
